package com.niuniu.rushdorm.controller;
/**
 * @author rambo
 * 2022/11/25
 */
import com.niuniu.rushdorm.exception.ReportedException;
import com.niuniu.rushdorm.pojo.entity.Clazz;
import com.niuniu.rushdorm.pojo.entity.Credential;
import com.niuniu.rushdorm.pojo.entity.User;
import com.niuniu.rushdorm.pojo.vo.UserVO;
import com.niuniu.rushdorm.pojo.response.Members;
import com.niuniu.rushdorm.pojo.response.MyRoom;
import com.niuniu.rushdorm.pojo.response.UserInfo;
import com.niuniu.rushdorm.pojo.vo.ResultVO;
import com.niuniu.rushdorm.service.*;
import com.niuniu.rushdorm.util.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    UserService userServiceImpl;
    @Autowired
    ClassService classServiceImpl;
    @Autowired
    CredentialService credentialService;
    @Autowired
    AuthService authService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    BedService bedService;

    @GetMapping("/user/myinfo")
    public ResultVO GetUserInfo(HttpServletRequest request){
        UserVO user = (UserVO) request.getAttribute("userVO");
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(user.getUid());
        userInfo.setStudentid(user.getStudent().getStudentId());
        userInfo.setName(user.getName());
        userInfo.setGender(user.getGender());
        User u =  userServiceImpl.GetUserById(userInfo.getUid());
        userInfo.setEmail(u.getEmail());
        userInfo.setTel(u.getTel());
        userInfo.setLast_login_time(user.getLastLoginTime());
        userInfo.setVerification_code(user.getStudent().getVerificationCode());
        Clazz clazz = classServiceImpl.GetClassById(user.getStudent().getClassId());
        if (clazz == null){
            return ResultVO.fail(ReportedException.build(StatusEnum.CLASS_NOT_EXIST));
        }
        userInfo.setClass_name(clazz.getName());
        return ResultVO.success(userInfo);
    }
    @PostMapping("/user/passwd")
    public ResultVO UpdatePasswd(HttpServletRequest request, @RequestBody Map<String,String> map){
        String oldPasswd = map.getOrDefault("oldPasswd","");
        String newPasswd = map.getOrDefault("newPasswd","");
        UserVO user = (UserVO) request.getAttribute("userVO");
        Credential c = authService.GetAuth(user.getUid());
        Long uid = credentialService.verifyCredential(c.getUsername(),oldPasswd);
        UserVO userVO = userServiceImpl.getUser(uid);
        if(userVO==null){
            throw ReportedException.build(StatusEnum.INVALID_USER);
        }
        authService.UpdatePsw(newPasswd,uid);
        redisTemplate.delete("access_token:"+String.valueOf(uid));
        redisTemplate.delete("refresh_token:"+String.valueOf(uid));
        return ResultVO.success(null);
    }
    @GetMapping("/user/myroom")
    public ResultVO GetRoom(HttpServletRequest request){
        UserVO user = (UserVO) request.getAttribute("userVO");
        MyRoom room = bedService.GetRoom(user.getUid());
        room.setMember(new Members());
        room.getMember().setRows(bedService.GetMember(room.getRoomId()));
        return ResultVO.success(room);
    }
}
